package com.book;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbManager {

	static String dburl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=book;SelectMethod=cursor";
	static String dbuser = "sa";
	static String dbpassword = "sa";

	static Connection conn = null;

	public static Connection getConnection() {

		if (conn != null) {
			return conn;
		}

		try {
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;

	}

	public static String getString() {
		return "this is from DbManager";

	}

	public static ResultSet getRst(String strsqlbuf) throws SQLException {
		ResultSet rst = null;
		PreparedStatement preState = null;

			preState = getConnection().prepareStatement(strsqlbuf.toString(),
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);

			rst = preState.executeQuery();
			
			conn.commit();

		return rst;
	}
	public static void exeSql(String sql){
		PreparedStatement preState=null;
		try {
			preState = getConnection().prepareStatement(sql.toString(),
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);

			 preState.executeUpdate();
			 conn.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
